import socket
import ssl

class client_ssl:
    def send_hello(self):
        CA_FILE = "ca-cert.pem"
        SERVER_CERT_FILE = "server-cert.pem"  # 服务器证书文件路径
        CLIENT_KEY_FILE = "client-key.pem"
        CLIENT_CERT_FILE = "client-cert.pem"

        # 创建SSL上下文对象
        context = ssl.SSLContext(ssl.PROTOCOL_TLS)
        context.check_hostname = False
        context.load_cert_chain(certfile=CLIENT_CERT_FILE, keyfile=CLIENT_KEY_FILE)
        context.load_verify_locations(CA_FILE)
        # context.verify_mode = ssl.CERT_REQUIRED
        
        context.verify_mode = ssl.CERT_NONE 
        context.load_verify_locations(cafile=CA_FILE)  # 设置根证书

        # 与服务端建立socket连接
        with socket.socket() as sock:
            # 将socket打包成SSL socket
            with context.wrap_socket(sock, server_side=False) as ssock:
                ssock.connect(('127.0.0.1', 10026))

                while True:
                    # 输入要发送的消息
                    msg = input("Enter a message to send (or 'quit' to exit): ")
                    if msg.lower() == 'quit':
                        break

                    # 向服务端发送消息
                    ssock.send(msg.encode("utf-8"))

                    # 接收并打印服务端返回的消息
                    response = ssock.recv(1024).decode("utf-8")
                    print(f"Received message from the server: {response}")

if __name__ == "__main__":
    client = client_ssl()
    client.send_hello()

